package com.woniu.adopt.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.adopt.entity.Activity;
import com.woniu.adopt.mapper.ActivityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ActivityService {

    @Autowired
    ActivityMapper activityMapper;

    //模糊查询 （活动名称，发布状态）+ 分页
    public PageInfo<Activity> queryActivityByPage(Integer now, Integer size, String name, String address, int display) {
        PageHelper.startPage(now, size);

        List<Activity> activities = activityMapper.selectActivityByName(name, address, display);
        for (Activity a : activities) {
            if (a.getActivityEndTime().getTime() < new Date().getTime()) {
                activityMapper.updateByPrimaryKey2(a.getId());
            }
        }

        //只可以分页下面的一个集合
        PageHelper.startPage(now, size);

        List<Activity> activities2 = activityMapper.selectActivityByName(name, address, display);
        return new PageInfo<>(activities2);
    }

    //模糊查询  已注销（活动名称）+ 分页
    public PageInfo<Activity> queryOldActivityByPage(Integer now, Integer size, String name, String address, int display) {
        PageHelper.startPage(now, size);

        List<Activity> activities = activityMapper.selectOldActivityByName(name, address, display);
        return new PageInfo<>(activities);
    }


    public int insertActivity(Activity activity) {
        //添加团队活动
        //时间转换为秒数，进行比较
        if (activity.getActivityStartTime().getTime() > activity.getInsertTime().getTime()) {
            int i = activityMapper.insert(activity);
            return i;
        } else {
            return 0;
        }
    }

    public Activity selectActivityByKey(int id) {
        return activityMapper.queryActivityByKey(id);
    }

    //修改活动信息，多层逻辑判断
    public int updateActivity(Activity activity) {
        //时间转换为秒数，进行比较
        //如果你  活动处于未删除 且 活动时间正常的状态，允许修改
        if (activity.getActivityStartTime().getTime() > activity.getUpdateTime().getTime() && activity.getDel() == 0) {
            int i = activityMapper.updateByPrimaryKey(activity);
            return i;
        } else if (activity.getDel() == -1) {
            //如果你  活动处于删除状态  也允许你随意修改其他信息，但是删除状态不可改
            int i = activityMapper.updateByPrimaryKey(activity);
            return i;
        } else {
            //不符合上述两条规则的修改，不允许修改，返回错误信息
            return 0;
        }
    }

    //这是逻辑删
    public int removeActivity(int id) {
        int i = activityMapper.updateByPrimaryKey2(id);
        return i;
    }

    //这是真删除
    public int removeActivity2(int id) {
        int i = activityMapper.deleteByPrimaryKey(id);
        return i;
    }

}
